﻿using System;


// XNA lib
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

//Oak Engine Lib
using OakEngine.GameState;
using OakEngine.Input;
using Microsoft.Xna.Framework.Input;

using MainJumpFleet.TempObjects;


namespace MainJumpFleet.GameStates
{
   public class ShootingGameScreenState :OAK_IGameState
    {
        #region OAK_IGameState Members
        SpriteBatch batch;
        SpriteFont mainMenuFont;
        Texture2D turret;
        Turret tur;
        ship ship;
        Vector2 mousepos;
        Vector2 forwardVector;
        float stopPos;
        float dt;

        Vector2 vec1, vec2;
        float speed;
        float acceleration;
       OakEngine.DebugTools.OAK_DrawLine line1;
       OakEngine.DebugTools.OAK_DrawLine line2;



        public ShootingGameScreenState()
        {
            Enter();
        }


        public void Enter()
        {
            mainMenuFont = CGame.CGame.GetInstance().Content.Load<SpriteFont>("Fonts\\mainMenuFont");
            turret = CGame.CGame.GetInstance().Content.Load<Texture2D>("Ships\\ship1_76x128");
            batch = CGame.CGame.GetInstance().SpriteBatch;
            line1 = new OakEngine.DebugTools.OAK_DrawLine(CGame.CGame.GetInstance().Device, new[] { Color.Red });
            line2 = new OakEngine.DebugTools.OAK_DrawLine(CGame.CGame.GetInstance().Device, new[] { Color.Green });


            tur = new Turret(turret);
            tur.SetPos(new Vector2(300, 200));
            //ship = new ship(CGame.CGame.GetInstance().Content.Load<Texture2D>("Ships\\ship2_77x128"), mainMenuFont);
            ship.position = new Vector2(500, 300);
            forwardVector = new Vector2(1, 0);


            speed = 0;
            acceleration = 100.0f;
        }
        float rotper = 1.0f;
        public bool Input(OAK_InputManager input)
        {
            if (input.Pressed(Keys.Escape) || input.Pressed(Buttons.Back, 0))
                CGame.CGame.GetInstance().ChangeState(new MainMenuState());

            if (speed <= -10.0f)
                speed = 20.0f;

            Vector2 dir = new Vector2(0, 0);

            if (input.LeftStickPosition(0).Y < 0.0f)
            {
                speed -= acceleration * dt;
            }
            if (input.LeftStickPosition(0).Y > 0.0f)
            {
                speed += acceleration * dt;
                
            }
            if (input.LeftStickPosition(0).X < 0.0f)
            {
                ship.SetLeftRotation(dt);
            }
            if (input.LeftStickPosition(0).X > 0.0f)
            {
                ship.SetRightRotation(dt);

            }





            dir.Y = speed;
             dir = OakEngine.AI.Tools.OAK_AITools.Vector2DRotate(dir, ship.roation);
             ship.dir = OakEngine.AI.Tools.OAK_AITools.Vector2DRotate(new Vector2(0,-1), ship.roation); ;
            ship.velocity = dir;

            if (input.Pressed(Buttons.A,0))
                ship.Fire(turret, ship.dir);
            
            return false;
        }
        public void Update(GameTime dt)
        {
            tur.SetTarget(ship.position);
            tur.Update();
            Vector2 offset = new Vector2(tur.m_vecTurrentPosition.X + 20,tur.m_vecTurrentPosition.Y + 20);


            // Turret
            line1.UpdateLinePosition(tur.m_vecTurrentPosition, ship.position);

            this.dt = (float)dt.ElapsedGameTime.TotalSeconds;

            // ship
            ship.Update(this.dt);

 
        }

        public void Render()
        {
            batch.DrawString(mainMenuFont, ship.velocity.ToString(), new Vector2(300, 250), Color.White);
            batch.DrawString(mainMenuFont, ship.roation.ToString(), new Vector2(300, 350), Color.Black);

            line1.DrawLine(batch);
            line2.DrawLine(batch);

            //if (ship.Intersect(tur.Bounds))
            //{
            //    tur.m_bisAlive = false;

            //}

                tur.Render(batch);



            ship.Draw(batch);
        }

        public void Exit()
        {
        }

        #endregion
    }
}
